package com.livra.examples.pickwebvoter.servlets;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.livra.model.Category;
import com.livra.model.Criterion;
import com.livra.model.Pick;

@SuppressWarnings("serial")
public class MethodsServlet extends AbstractPickWebVoterServlet {
    
    
    /**
     * this method dispatch all the actions.
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        setComboInformation(req);
        if (isAction(ServletConstants.GET_CATEGORIES_ACTION)) {
            List<Category> categories = this.livraService.getCategories();
            req.setAttribute("categories", categories);
            req.getRequestDispatcher(ServletConstants.MAIN_VIEW).forward(req, resp);
            return;
        }
        if (isAction(ServletConstants.START_SEARCH_PICK_ACTION)) {
            req.setAttribute("action", ServletConstants.START_SEARCH_PICK_ACTION);
            req.getRequestDispatcher(ServletConstants.MAIN_VIEW).forward(req, resp);
            return;
        }
        if (isAction(ServletConstants.START_SEARCH_PICKS_ACTION)) {
            startSearchPicksAction(req, resp);
            return;
        }
        if (isAction(ServletConstants.PERFORM_SEARCH_PICKS_ACTION)) {
            searchPicksAction(req, resp);
            return;
        }
        if (isAction(ServletConstants.PERFORM_SEARCH_PICK_ACTION)) {
            searchPickAction(req, resp);
            return;
        }
    }

    private void searchPickAction(HttpServletRequest req,
            HttpServletResponse resp) throws IOException, ServletException {
        req.setAttribute("pick", this.livraService.getPick(Integer.parseInt(req.getParameter("pick_id"))));
        req.setAttribute("action",ServletConstants.RESULT_PICK_ACTION);
        req.getRequestDispatcher(ServletConstants.MAIN_VIEW).forward(req, resp);
    }

    private void startSearchPicksAction(HttpServletRequest req,
            HttpServletResponse resp) throws IOException, ServletException {
        req.setAttribute("action", ServletConstants.START_SEARCH_PICKS_ACTION);
        setComboInformation(req);
        req.getRequestDispatcher(ServletConstants.MAIN_VIEW).forward(req, resp);
    }

    private void searchPicksAction(HttpServletRequest req,
            HttpServletResponse resp) throws IOException, ServletException {
        req.setAttribute("action", ServletConstants.START_SEARCH_PICKS_ACTION);
        
        String categoryId = req.getParameter("categories");
        System.out.println(categoryId);
        List<Pick> picks = livraService.getPicks(getAsCriterion(req.getParameter("criterion")), Integer.parseInt(categoryId));
        System.out.println(picks.size());
        req.setAttribute("picks",picks);
        //req.setAttribute("action", ServletConstants.RESULT_PICKS_ACTION);
        req.getRequestDispatcher(ServletConstants.MAIN_VIEW).forward(req, resp);
    }


    private Criterion getAsCriterion(String parameter) {
        for (Criterion criterion : Criterion.values()) {
           if (criterion.name().equalsIgnoreCase(parameter))
               return criterion;
        }
        return null;
    }

    private Map<String,String> getCriterionAsMap() {
        Map<String,String> map = new HashMap<String,String>();
        for (Criterion criterion : Criterion.values()) {
            map.put(criterion.name(), criterion.name());
        }
        return map;
    }
    
    public Map<String,String> getCategoriesAsMap(List<Category> categories) {
        Map<String,String> map = new HashMap<String,String>();
        for (Category category : categories) {
            map.put(category.getId(), category.getName());
        }
        return map;
        
    }
    
    private void setComboInformation(HttpServletRequest req) throws IOException {
        req.setAttribute("criterions", getCriterionAsMap());
        req.setAttribute("categories", getCategoriesAsMap(this.livraService.getCategories()));
    }

    
}
